package com.zhao.Others;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.util.ClassUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImgOperationTest {
    public static String staticPath = "/D:/IdeaProjects/NewEbickManagement/target/classes/static/";
    public static void main(String[] args) throws IOException {
        //System.out.println(staticPath);
        //读入文件
        File file = new File(staticPath + "TestImg/ebick1.jpg" );
        System.out.println(file.exists());
        System.out.println(file);
        //构造Image对象
        BufferedImage src = ImageIO.read(file);
        System.out.println(src);
        //得到源图宽
        int width = src.getWidth(null);
        //得到源图长
        int height = src.getHeight(null);
        System.out.println(width+" "+height);
        //限制最小宽高
       /* final int leastWidth = 448;
        //限制最大宽高
        final int biggestHeight = 1024;

        //转变后宽
        int xwidth;
        if (width < leastWidth || width > biggestHeight) {
            xwidth = 500;
        } else {
            xwidth = width;
        }

        //转变后高
        int yheight;
        if (height < leastWidth || width > biggestHeight) {
            yheight = 500;
        } else {
            yheight = width;
        }

        BufferedImage tag = new BufferedImage(xwidth, yheight, BufferedImage.TYPE_INT_RGB);
        //绘制缩小后的图
        tag.getGraphics().drawImage(src, 0, 0, xwidth, yheight, null);
        //输出到文件流
        FileOutputStream out = new FileOutputStream(staticPath + "TestImg/ebick2.jpg");

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        //近JPEG编码
        encoder.encode(tag);

        out.close();
        System.out.println("完成拉伸！");*/

    }
}
